#ifndef _P2P_UPGRADE_H_
#define _P2P_UPGRADE_H_

#define ID_TYPE(x) (x>>16)
#define BOOTLOADER_ID 0
#define USERDB_ID 0x04FB
#define TEMPDB_ID 0x05FA

enum{
    UPGRADE_MULTI,
    UPGRADE_SINGLE
};

//#ifdef ENABLE_REMOTE_FLASH
#define UPGRADE_MODE_One2One		0x51
#define UPGRADE_MODE_MUL_NORMAL		0x61
#define UPGRADE_MODE_MUL_COMPRESS	0x62

typedef struct _EROM_UPGRADE_PARAM
{
    UINT32 upgrade_mode;    // byte[0]:p2p or p2m, 
                            // byte[1]:uart bitrate, 1: 2M, 2:6M, else 115200
                            // byte[2-3]:reserved
    UINT32 binary_len;
    UINT32 unzip_source_addr;
    UINT32 unzip_temp_addr;
//    UINT32 image_addr;
}EROM_UPGRADE_PARAM, *PEROM_UPGRADE_PARAM;
#define EUP_OFFSET(field)   ((UINT32)&(((PEROM_UPGRADE_PARAM)0)->field))
#define UPGRADE_PARAM_ADDR  0xa0000100
#define UPGRADE_PARAM_ADDR_EXT  0xa00001F0

typedef struct _CMD_VERSION_HEAD
{
    UINT32 flash_size;

    UINT32 fw_status    : 8;
    UINT32 reserved     : 8;
    UINT32 data_len     : 16;
}CMD_VERSION_HEAD, *PCMD_VERSION_HEAD;
//#endif

struct upge_feature_config
{
    UINT8 sys_sdram_size;//2:2M,8:8M....
    char *bk_buff;
    UINT8 remote_flash_read_tmo;
    UINT8 remote_flash_write_verify;
    UINT8 enable_serial_flash;
    UINT8 enable_fast_erom_upgrade;
    UINT8 project_fe_is_dvbt;
    UINT8 enable_lzma_out_read;
    UINT32 chip_flashwr;
    UINT32 chip_config;

    INT32 (*InitBlockList)();
    void (*FreeBlockList)();
    void (*FreeSlavelist)();
    UINT32 (*Index2Id)(INT32 index);
    void (*ClearUpgFlag)();
    INT32 (*SetUpgFlag)(UINT32 id, UINT32 mask, INT32 flag);
    void (*p2p_delay)(void);
    
    INT32 (*sys_upgrade)(void (*callback)(INT32 type, INT32 process, UINT8 *str), UINT32 (*GetExitKey)(void));
    void (*SetUpgradeMode)(INT32 mode);
};

INT32 InitBlockList();
INT32 InitBlockList_ext(struct upge_feature_config *pupge_feature_config);
INT32 InitBlockList_ext_normal(struct upge_feature_config *pupge_feature_config);

void FreeBlockList();
void FreeSlavelist();
UINT32 Index2Id(INT32 index);
void ClearUpgFlag();
INT32 SetUpgFlag(UINT32 id, UINT32 mask, INT32 flag);
void p2p_delay(void);
void p2p_stop_slave_command(void);
#ifndef ENABLE_FAST_EROM_UPGRADE
static INT32 send2Back_tm(INT8 * buff, INT32 len, INT32 timeout);
static INT32 sendData( INT8 *buff, INT32 len );
static INT32 readData(INT8 * buff, INT32 len, INT32 timeout);
static INT32 IsInSlave(unsigned int id);
static INT32 SlaveReorg(void (*callback)(INT32 type, INT32 process, UINT8 *str));
static INT32 command_comtest();
static INT32 command_version (void (*callback)(INT32 type, INT32 process, UINT8 *str));
static INT32 command_address (void (*callback)(INT32 type, INT32 process, UINT8 *str));
static INT32 command_move (INT32 index, UINT32 offset, void (*callback)(INT32 type, INT32 process, UINT8 *str));
static INT32 command_transfer (INT32 index, void (*callback)(INT32 type, INT32 process, UINT8 *str));
static INT32 command_burn(void (*callback)(INT32 type, INT32 process, UINT8 *str));
static INT32 command_reboot();
#endif
INT32 sys_upgrade(void (*callback)(INT32 type, INT32 process, UINT8 *str), UINT32 (*GetExitKey)(void));
INT32 sys_upgrade2(void (*callback)(INT32 type, INT32 process, UINT8 *str), UINT32 (*GetExitKey)(void));
void SetUpgradeMode(INT32 mode);
void p2p_uart_set_id(UINT32 uart_id);

#endif /* _P2P_UPGRADE_H_ */

